package com.wyd.resourcetj.service;


import java.util.Calendar;
import java.util.Date;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import com.wyd.resourcetj.dao.IConsume_LogDao;
import com.wyd.resourcetj.dao.IMemberDao;
import com.wyd.resourcetj.model.Consume_Log;
import com.wyd.resourcetj.model.Member;

import org.springframework.stereotype.Service;


/**
 * Consume_LogServiceImpl
 */
@Service
public class Consume_LogServiceImpl implements IConsume_LogService {
    @Resource
    private IMemberDao memberDao;

    @Resource
    private IConsume_LogDao  consume_LogDao;


    @Transactional
    @Override
    public Consume_Log swipingCard(Integer read_room_id,String card_no, boolean inOrOut) {
        Member m = memberDao.getCardStatus(card_no);

        Consume_Log cl=null;
        if(inOrOut){//如果是刷卡进入
            if(m.getStatus()==0){//状态正常
                //记录刷卡信息

                cl=new Consume_Log();
                cl.setCard_no(card_no);
                cl.setRead_room_id(read_room_id);
                 //设置离开时间为进入时间+30分钟
                 Calendar nowTime = Calendar.getInstance();
                 nowTime.add(Calendar.MINUTE, 30);
                cl.setOut_time( nowTime.getTime());

                cl=consume_LogDao.save(cl);

             //修改会员卡状态为资源室id
             m.setStatus(read_room_id);
             memberDao.save(m);
                

            
               
            }else{
                //状态异常，联系管理员
                System.out.println("会员卡状态异常，请联系管理员");
              //  memberDao.updateStatus(cardNo, 0 );

               

                
            }
        }else{//如果是刷卡离开
           
              //查询会员最后一次进入资源室的日志编号
            cl = consume_LogDao.getMaxInTime(card_no);
           
       //更新离开时间
       consume_LogDao.updateOutTime(cl.getId(),new Date());


            //修改会员卡状态为0
           // memberDao.updateStatus(cardNo, 0 );
           m.setStatus(0);

           
           
        }
        return cl;//返回ConsumeLog对象
    }

    
}